<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Express as ExpressModel;
use think\facade\View;
use app\admin\middleware\Auth;
use think\exception\ValidateException;
use app\common\validate\Express as ExpressValidate;

class Express extends BaseController
{
    protected $middleware = [
        Auth::class
    ];

    public function index()
    {
        $nav_title = [
            'nav_title' => '配送平台',
            'title' => '配送平台'
        ];

        // 接收查询字段
        $SearchField = $this->request->param('search_field','name');

        $keyword = $this->request->param('keyword','');

        $where = [];

        if(!empty($SearchField) && !empty($keyword))
        {
            $where[] = [$SearchField,'like',"%$keyword%"];
        }else{
            $keyword = '请输入相应的内容';
        }

        // 查询数据
        $ExpressList = ExpressModel::where($where)->paginate(['list_rows' => 5,'query' => request()->param()]);

        // 编号 currentPage 当前页 listRows 每页要显示多少条数据
        $num = ($ExpressList->currentPage() - 1) * $ExpressList->listRows();
        
        return View::fetch('',[
            'nav_title' => $nav_title,
            'ExpressList' => $ExpressList,
            'num' => $num,
            'SearchField' => $SearchField,
            'keyword' => $keyword
        ]);
    }

    // 添加
    public function add()
    {
        $nav_title = [
            'nav_title' => '配送平台',
            'title' => '新增配送平台'
        ];

        if($this->request->isPost())
        {
            $params = $this->request->param();

            $data = [
                'name' => $params['name']
            ];

            // 验证数据
            try {
                validate(ExpressValidate::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getError());
            }

            $Express = new ExpressModel();

            $result = $Express->save($data);

            if($result !== true)
            {
                $this->error('新增失败');
            }else {
                $this->success('新增成功',(String)url('admin/express/index'));
            }
        }

        return View::fetch('',[
            'nav_title' => $nav_title,
        ]);
    }

    public function edit()
    {
        $nav_title = [
            'nav_title' => '配送平台',
            'title' => '修改配送平台'
        ];

        $expressid = $this->request->param('expressid');

        $express = ExpressModel::find($expressid);

        if(!$express)
        {
            $this->error('该配送平台不存在');
        }

        if($this->request->isPost())
        {
            $params = $this->request->param();

            $data = [
                'name' => $params['name'],
            ];

            $result = $express->save($data);

            if($result !== true)
            {
                $this->error('修改失败');
            }else {
                $this->success('修改成功',(String)url('admin/express/index'));
            }
        }

        return View::fetch('',[
            'nav_title' => $nav_title,
            'express' => $express
        ]);
    }


    public function del()
    {
        if($this->request->isAjax())
        {
            $expressid = $this->request->param('expressid');

            if(empty($expressid))
            {
                $this->error('请选择要删除记录');
            }

            if(is_array($expressid))
            {
                // 批量删除操作

                // 查询数据
                $idArr = ExpressModel::whereIn('id',$expressid)->column('id');

                $NotId = '';
                
                foreach ($expressid as $item) {
                    if(!in_array($item,$idArr))
                    {
                        $NotId .= $item . ',';
                    }
                }
                
                if(!empty($NotId))
                {
                    $NotId = rtrim($NotId,',');
                    $this->error("ID：{$NotId}不存在，请重新选择");
                }

                $result = ExpressModel::destroy($expressid);

                if($result !== true)
                {
                    $this->error('批量删除失败');
                }else{
                    $this->success('批量删除成功');
                }


            }else{
                // 单条删除

                $Express = ExpressModel::find($expressid);

                if(!$Express)
                {
                    $this->error('该配送平台不存在，请重新选择');
                }

                $result = ExpressModel::destroy($expressid);

                if($result !== true)
                {
                    $this->error('删除失败');
                }else{
                    $this->success('删除成功');
                }

            }
        }
    }
}
